** Replication Code for:
** Terrorism Activates Ethnocentrism to Explain Greater Willingness to 
** Sacrifice Civil Liberties: Evidence from Germany
** Authors: Christina Novak Hansen and Peter Thisted Dinesen 
** Date: 10/02/2021

*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
*3. Results for main text
*-------------------------------------------------------------------------------

clear all
use GGSS2016_analysis.dta
svyset xs11 [pweight=wghtpew], vce(linearized)

keep if german==1 & governmentsplit!=.					

********************************************************************************


								***Table 1***

*Full ethnocentrism index and civil liberties with reference to terrorism
svy: regress st_terrori c.st_ethno i.sex st_age st_age2 i.proedu2 i.work2 if treatment1!=.
outreg2 using Table_1.xls, drop(i.sex st_age st_age2 i.proedu2 i.work2) nocons stats(coef se) dec(2) symbol(**,*) alpha(0.01,0.05) label replace 
svy: regress st_terrori c.st_ethno i.treatment1 i.sex st_age st_age2 i.proedu2 i.work2 if treatment1!=. 
outreg2 using Table_1.xls, drop(i.sex st_age st_age2 i.proedu2 i.work2) nocons stats(coef se) dec(2) symbol(**,*) alpha(0.01,0.05) label append 
svy: regress st_terrori c.st_ethno##i.treatment1 i.sex st_age st_age2 i.proedu2 i.work2 if treatment1!=. 
outreg2 using Table_1.xls, drop(i.sex st_age st_age2 i.proedu2 i.work2) nocons stats(coef se) dec(2) symbol(**,*) alpha(0.01,0.05) label append

*Full ethnocentrism index and civil liberties without reference to terrorism
svy: regress st_surveillanceindex c.st_ethno i.sex st_age st_age2 i.proedu2 i.work2 if treatment1!=. 
outreg2 using Table_1.xls, drop(i.sex st_age st_age2 i.proedu2 i.work2) nocons stats(coef se) dec(2) symbol(**,*) alpha(0.01,0.05) label append
svy: regress st_surveillanceindex c.st_ethno i.treatment1 i.sex st_age st_age2 i.proedu2 i.work2 if treatment1!=. 
outreg2 using Table_1.xls, drop(i.sex st_age st_age2 i.proedu2 i.work2) nocons stats(coef se) dec(2) symbol(**,*) alpha(0.01,0.05) label append 
svy: regress st_surveillanceindex c.st_ethno##i.treatment1 i.sex st_age st_age2 i.proedu2 i.work2 if treatment1!=. 
outreg2 using Table_1.xls, drop(i.sex st_age st_age2 i.proedu2 i.work2) nocons stats(coef se) dec(2) symbol(**,*) alpha(0.01,0.05) label append

*Authorchild and civil liberties with reference to terrorism
svy: regress st_terrori c.st_authorchild i.sex st_age st_age2 i.proedu2 i.work2 if treatment1!=. 
outreg2 using Table_1.xls, drop(i.sex st_age st_age2 i.proedu2 i.work2) nocons stats(coef se) dec(2) symbol(**,*) alpha(0.01,0.05) label append
svy: regress st_terrori c.st_authorchild i.treatment1 i.sex st_age st_age2 i.proedu2 i.work2 if treatment1!=. 
outreg2 using Table_1.xls, drop(i.sex st_age st_age2 i.proedu2 i.work2) nocons stats(coef se) dec(2) symbol(**,*) alpha(0.01,0.05) label append 
svy: regress st_terrori c.st_authorchild##i.treatment1 i.sex st_age st_age2 i.proedu2 i.work2 if treatment1!=. 
outreg2 using Table_1.xls, drop(i.sex st_age st_age2 i.proedu2 i.work2) nocons stats(coef se) dec(2) symbol(**,*) alpha(0.01,0.05) label append
*Interaction model including both ethnocentrism and authoritarianism
svy: regress st_terrori c.st_ethno##i.treatment1 c.st_authorchild##i.treatment1 i.sex st_age st_age2 i.proedu2 i.work2 
outreg2 using Table_1.xls, drop(i.sex st_age st_age2 i.proedu2 i.work2) nocons stats(coef se) dec(2) symbol(**,*) alpha(0.01,0.05) label append




			****Interaction effect for different levels of ethnocentrism***
			
*Calculate percentiles based on sample used in the analyses (see below). 
svy: regress st_terrori c.st_ethno##i.treatment1 i.sex st_age st_age2 i.proedu2 i.work2
sum st_ethno if e(sample), detail

*Interaction effect for different levels of ethnocentrism for pre- and post-attacks
*groups 
*0, 50, and 95th percentiles, control and treatment
margins, at(treatment1=(0) st_ethno=(0.1166667 .3833333 0.775)) vce(unconditional)
margins, at(treatment1=(1) st_ethno=(0.1166667 .3833333 0.775)) vce(unconditional)
di .9040029-.4591432

*Difference/change from pre-attacks to post-attacks group across different 
*ethnocentrism levels 
margins, dydx(treatment1) at(st_ethno=(0.1166667))
margins, dydx(treatment1) at(st_ethno=(.38333))
margins, dydx(treatment1) at(st_ethno=(0.775))



								****Figure 1***

*Marginsplot for different percentiles on the ethnocentrism variable 
svy: regress st_terrori c.st_ethno##i.treatment1 i.sex st_age st_age2 i.proedu2 i.work2
sum st_ethno if e(sample), detail
margins, at( treatment1=(0,1) st_ethno=(0.1166667, .3833333, 0.775)) vce(unconditional)
 marginsplot, xdimension(st_ethno treatment1, ///
 elabels(1 "5th percentile" 2 " " 3 "50th percentile" 4 " " 5 "95th percentile" 6 " " )) ///
 horizontal plotopts(connect(none)) ytitle("") yscale(reverse) yline(2.5 4.5, ///
 lwidth(vvthin)) ylabel(, grid glcolor(gs14)) xtitle("") ///
 xlabel(, grid glcolor(gs14)) xmtick(##2, grid glcolor(gs14)) ///
 title("""", size(medium)) ///
 legend(on order(2 "Predicted mean for pre-attacks group" 1 "95 % confidence interval") cols(1) region(lcolor(none)))
 
graph export Figure_1.pdf, replace

*Change symbol of the post-attacks group in the graph-editor.


								****Figure 2***
/*Rolling window regressions (full description in Appendix P and matching Do-file "Appendix_P":
We generate a set of pre-attacks and post-attacks groups, where the "window" 
of time  - meaning the 7 consequetive days after the attacks - for the 
post-attack treatment is moved by 1 day at the
time. Each moving window is compared to the pre-attacks group.*/

*1: Generating windows for dummy variables
gen window1 = 1 if respid!=. & (edate>=td(15/07/2016) & edate<=td(21/07/2016))
gen window2 = 1 if respid!=. & (edate>=td(16/07/2016) & edate<=td(22/07/2016))
gen window3 = 1 if respid!=. & (edate>=td(17/07/2016) & edate<=td(23/07/2016))
gen window4 = 1 if respid!=. & (edate>=td(18/07/2016) & edate<=td(24/07/2016))
gen window5 = 1 if respid!=. & (edate>=td(19/07/2016) & edate<=td(25/07/2016))
gen window6 = 1 if respid!=. & (edate>=td(20/07/2016) & edate<=td(26/07/2016))
gen window7 = 1 if respid!=. & (edate>=td(21/07/2016) & edate<=td(27/07/2016))
gen window8 = 1 if respid!=. & (edate>=td(22/07/2016) & edate<=td(28/07/2016))
gen window9 = 1 if respid!=. & (edate>=td(23/07/2016) & edate<=td(29/07/2016))
gen window10 = 1 if respid!=. & (edate>=td(24/07/2016) & edate<=td(30/07/2016))
gen window11 = 1 if respid!=. & (edate>=td(25/07/2016) & edate<=td(31/07/2016))
gen window12 = 1 if respid!=. & (edate>=td(26/07/2016) & edate<=td(01/08/2016))
gen window13 = 1 if respid!=. & (edate>=td(27/07/2016) & edate<=td(02/08/2016))
gen window14 = 1 if respid!=. & (edate>=td(28/07/2016) & edate<=td(03/08/2016))
gen window15 = 1 if respid!=. & (edate>=td(29/07/2016) & edate<=td(04/08/2016))
gen window16 = 1 if respid!=. & (edate>=td(30/07/2016) & edate<=td(05/08/2016))
gen window17 = 1 if respid!=. & (edate>=td(31/07/2016) & edate<=td(06/08/2016))
gen window18 = 1 if respid!=. & (edate>=td(01/08/2016) & edate<=td(07/08/2016))
gen window19 = 1 if respid!=. & (edate>=td(02/08/2016) & edate<=td(08/08/2016))
gen window20 = 1 if respid!=. & (edate>=td(03/08/2016) & edate<=td(09/08/2016))
gen window21 = 1 if respid!=. & (edate>=td(04/08/2016) & edate<=td(10/08/2016))
gen window22 = 1 if respid!=. & (edate>=td(05/08/2016) & edate<=td(11/08/2016))
gen window23 = 1 if respid!=. & (edate>=td(06/08/2016) & edate<=td(12/08/2016))
gen window24 = 1 if respid!=. & (edate>=td(07/08/2016) & edate<=td(13/08/2016))
gen window25 = 1 if respid!=. & (edate>=td(08/08/2016) & edate<=td(14/08/2016))
gen window26 = 1 if respid!=. & (edate>=td(09/08/2016) & edate<=td(15/08/2016))
gen window27 = 1 if respid!=. & (edate>=td(10/08/2016) & edate<=td(16/08/2016))
gen window28 = 1 if respid!=. & (edate>=td(11/08/2016) & edate<=td(17/08/2016))
gen window29 = 1 if respid!=. & (edate>=td(12/08/2016) & edate<=td(18/08/2016))
gen window30 = 1 if respid!=. & (edate>=td(13/08/2016) & edate<=td(19/08/2016))
gen window31 = 1 if respid!=. & (edate>=td(14/08/2016) & edate<=td(20/08/2016))
gen window32 = 1 if respid!=. & (edate>=td(15/08/2016) & edate<=td(21/08/2016))
gen window33 = 1 if respid!=. & (edate>=td(16/08/2016) & edate<=td(22/08/2016))
gen window34 = 1 if respid!=. & (edate>=td(17/08/2016) & edate<=td(23/08/2016))
gen window35 = 1 if respid!=. & (edate>=td(18/08/2016) & edate<=td(24/08/2016))
gen window36 = 1 if respid!=. & (edate>=td(19/08/2016) & edate<=td(25/08/2016))
gen window37 = 1 if respid!=. & (edate>=td(20/08/2016) & edate<=td(26/08/2016))
gen window38 = 1 if respid!=. & (edate>=td(21/08/2016) & edate<=td(27/08/2016))
gen window39 = 1 if respid!=. & (edate>=td(22/08/2016) & edate<=td(28/08/2016))
gen window40 = 1 if respid!=. & (edate>=td(23/08/2016) & edate<=td(29/08/2016))
gen window41 = 1 if respid!=. & (edate>=td(24/08/2016) & edate<=td(30/08/2016))
gen window42 = 1 if respid!=. & (edate>=td(25/08/2016) & edate<=td(31/08/2016))




foreach var of varlist window* {
	gen c`var' = `var'
}

*Replacing missing values in dummy variables with 0 for observations in the pre-
*attacks group.

foreach var of varlist cwindow* {
	replace `var' = 0 if treatment1==0	
}

save GGSS2016_matched_windows.dta, replace


********************************************************************************

*2: Estimating main model for each window-dummy and storing results


*Ethnocentrism
tempname e_t_C
postfile `e_t_C'  e_t_c_coef e_t_c_SE e_t_c_pval using e_t_C.dta, replace
foreach var of varlist cwindow*{
	qui svy: regress st_terrori c.`var'##c.st_ethno i.sex st_age st_age2 i.proedu2 i.work2
	matrix define temp5=r(table)
	post `e_t_C'  (`=_b[`var'#st_ethno]') (`=_se[`var'#st_ethno]') (`=temp5[4,3]')
}
postclose `e_t_C'


*Authorchild (not shown in Appendix)
tempname ac_t_C
postfile `ac_t_C'  ac_t_c_coef ac_t_c_SE ac_t_c_pval using ac_t_C.dta, replace
foreach var of varlist cwindow*{
	qui svy: regress st_terrori c.`var'##c.st_authorchild i.sex st_age st_age2 i.proedu2 i.work2
	matrix define temp5=r(table)
	post `ac_t_C'  (`=_b[`var'#st_authorchild]') (`=_se[`var'#st_authorchild]') (`=temp5[4,3]')
}
postclose `ac_t_C'


*Left-right (not shown in Appendix)
tempname lr_t_C
postfile `lr_t_C'  lr_t_c_coef lr_t_c_SE lr_t_c_pval using lr_t_C.dta, replace
foreach var of varlist cwindow*{
	qui svy: regress st_terrori c.`var'##c.st_lrID i.sex st_age st_age2 i.proedu2 i.work2
	matrix define temp5=r(table)
	post `lr_t_C'  (`=_b[`var'#st_lrID]') (`=_se[`var'#st_lrID]') (`=temp5[4,3]')
}
postclose `lr_t_C'





********************************************************************************

*3: Merging datasets into one dataset

*Merging ethnocentrism and authorchild datasets
clear
use e_t_C.dta 
gen startday =_n
save e_t_C.dta, replace

clear
use ac_t_C.dta
gen startday =_n
merge 1:1 startday using e_t_C.dta
drop _merge
save e_ac_t_C.dta, replace

*Merging with authorleaders
clear 
use lr_t_C.dta
gen startday =_n
merge 1:1 startday using e_ac_t_C.dta
drop _merge

********************************************************************************

*4: Generating variables for 95% CI's, upper and lower limits
*Ethnocentrism
gen e_c_LB = e_t_c_coef - 1.96*e_t_c_SE
gen e_c_UB = e_t_c_coef + 1.96*e_t_c_SE


*Authorchild
gen ac_c_LB = ac_t_c_coef - 1.96*ac_t_c_SE
gen ac_c_UB = ac_t_c_coef + 1.96*ac_t_c_SE


*Left-right self-placement
gen lr_c_LB = lr_t_c_coef - 1.96*lr_t_c_SE
gen lr_c_UB = lr_t_c_coef + 1.96*lr_t_c_SE

order startday ///
	e_t_c_coef e_t_c_SE e_c_LB e_c_UB e_t_c_pval ///
	ac_t_c_coef ac_t_c_SE ac_c_LB ac_c_UB ac_t_c_pval ///
	lr_t_c_coef lr_t_c_SE lr_c_LB lr_c_UB lr_t_c_pval 

save Time_figure.dta, replace




********************************************************************************

*5: Plotting the estimatet coefficients and calculated 95%-CIs


								***Figure P1***


*The activation of ethnocentrism in the first three weeks after the Normandy 
*terrorist attack

clear 
use Time_figure.dta

gen twodays=!1 if inlist(startday, 2, 4, 6 , 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42)
	
twoway (connected e_t_c_coef startday if startday<=32, mcolor(gs3) msymbol(square_hollow) lcolor(gs3)) ///
	(scatter e_t_c_coef startday if startday<=32 & e_t_c_pval<=0.05, mcolor(gs3) msymbol(square)), ///
	ytitle(Treatment effect (Interaction Term)) yline(0, lcolor(black)) ylabel(-0.4(0.2)0.8, labsize(medsmall) ///
	angle(horizontal) grid) xtitle(Days after Nice Terrorist Attack) xtitle(, size(medsmall)) ///
	xscale(range(0 30)) xscale(nofextend) xline(0 4 8 10 12, lcolor(black)) ///
	xlabel(0(2)32, labels grid) xmtick(none, labels) ///
	legend(order(1 "Ethnocentrism (insignificant)" 2 "Ethnocentrism (significant)") size(medsmall) region(lcolor(none))) xsize(7)
	
*Write city/town names for terrorist attacks manually in the graph editor.

graph export Figure_2.pdf, replace




 
